package com.signway.swmediaplayerdemo.test;

import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.util.Log;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Random;

public class Text {
    public static final String TAG = Text.class.getName();

    public static final String VIDEO = "video/";
    public static final String AUDIO = "audio/";

    public MediaExtractor mAudioExtractor = null;
    public MediaExtractor mVideoExtractor = null;

    public boolean exit = false;

    private long textAudioCounter = 0;
    private long textAudioErrCounter = 0;
    private long textVideoCounter = 0;
    private long textVideoErrCounter = 0;

    public void mediaExtractorText(final String filePath) {
        exit = false;

        new Thread(() -> {
            while (!exit) {
                textAudioCounter++;
                try {
                    Random rand = new Random();
                    mAudioExtractor = setNextExtractor(mAudioExtractor, filePath, AUDIO);
                    int counter = rand.nextInt(100);
                    while (counter-- > 0) {
                        if (mAudioExtractor.getSampleFlags() < 0) {
                            break;
                        }
                        ByteBuffer inputBuffer = ByteBuffer.allocate(1024 * 1024);
                        int sampleSize = mAudioExtractor.readSampleData(inputBuffer, 0);
                        int sampleIndex = mAudioExtractor.getSampleTrackIndex();
                        long presentationTimeUs = mAudioExtractor.getSampleTime();
                        if (sampleSize < 0) {
                            Log.d(TAG, "audio sampleSize " + sampleSize + ", presentationTimeUs " + presentationTimeUs);
                            textAudioErrCounter++;
                            break;
                        }
                        mAudioExtractor.advance();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(() -> {
            while (!exit) {
                textVideoCounter++;
                try {
                    Random rand = new Random();
                    mVideoExtractor = setNextExtractor(mVideoExtractor, filePath, VIDEO);
                    int counter = rand.nextInt(100);
                    while (counter-- > 0) {
                        if (mVideoExtractor.getSampleFlags() < 0) {
                            break;
                        }
                        ByteBuffer inputBuffer = ByteBuffer.allocate(1024 * 1024);
                        int sampleSize = mVideoExtractor.readSampleData(inputBuffer, 0);
                        int sampleIndex = mVideoExtractor.getSampleTrackIndex();
                        long presentationTimeUs = mVideoExtractor.getSampleTime();
                        if (sampleSize < 0) {
                            Log.d(TAG, "audio sampleSize " + sampleSize + ", presentationTimeUs " + presentationTimeUs);
                            textVideoErrCounter++;
                            break;
                        }
                        mVideoExtractor.advance();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(() -> {
            while (!exit) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.d(TAG, "textVideoCounter " + textVideoCounter + ", textAudioCounter " + textAudioCounter +
                                ", textVideoErrCounter " + textVideoErrCounter + ", textAudioErrCounter " + textAudioErrCounter);
            }
        }).start();
    }

    public void exit() {
        exit = true;
    }

    public MediaExtractor setNextExtractor(MediaExtractor extractor, String path, String type) throws IOException {
        if (extractor != null) {
            extractor.release();
        }
        MediaExtractor mediaExtractor = new MediaExtractor();
        mediaExtractor.setDataSource(path);
        Log.d(TAG, "setDataSource " + path);
        Log.d(TAG, "getTrackCount: " + mediaExtractor.getTrackCount());
        for (int i = 0; i < mediaExtractor.getTrackCount(); i++) {
            MediaFormat format = mediaExtractor.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);
            Log.d(TAG, "format : " + format + ", mime: " + mime);
            if (mime.startsWith(type)) {
                mediaExtractor.selectTrack(i);
                break;
            }
        }
        return mediaExtractor;
    }
}
